
package com.tianyang.yukuaifuzs.common.utils;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.tianyang.yukuaifuzs.common.view.SelectPopWindow;
import com.tianyang.yukuaifuzs.module.wheel.RegionWheel;
import com.tianyang.yukuaifuzs.module.wheel.interfaces.IWheelRegionChangeCallback;

/**
 * Description : 省市区事件处理
 */
public class RegionCheckListener {

    private static final int INDEX_PROVINCE = 0;

    private static final int INDEX_CITY = 1;

    private static final int INDEX_REGION = 2;

    private final Activity context;

    public RegionCheckListener(Activity context) {
        this.context = context;
    }

    /**
     * 处理选择省市
     *
     * @param parentView 父控件
     * @param showView 显示的控件
     * @param iWheelRegionChangeCallback 回调
     */
    public void handleRegionCheck(View parentView, TextView showView,
                                  IWheelRegionChangeCallback iWheelRegionChangeCallback) {
        if (parentView == null) {
            return;
        }
        RegionWheel regionWheel;
        if (!TextUtils.isEmpty(showView.getText().toString())) {
            String[] regions = showView.getText().toString().split("\\s");
            if (regions.length > 2) { // 省市区全
                regionWheel = new RegionWheel(context, null, regions[INDEX_PROVINCE],
                        regions[INDEX_CITY], regions[INDEX_REGION], true);
            } else if (regions.length == 2) {// 特殊的，只有省份如澳门，香港
                regionWheel = new RegionWheel(context, null, regions[INDEX_PROVINCE], regions[INDEX_CITY], null, true);
            } else {
                regionWheel = new RegionWheel(context, true);
            }
        } else {
            regionWheel = new RegionWheel(context, true);
        }
        SelectPopWindow pw = new SelectPopWindow(context, parentView,
                regionWheel.getAreaView());
        RegionClickListener regionClickListener = new RegionClickListener(pw, showView,
                iWheelRegionChangeCallback);
        regionWheel.setWheelFinishListener(regionClickListener);
        pw.setConfirmListener(regionClickListener);
    }

    /**
     * 处理选择省市
     *
     * @param parentView 父控件
     * @param showView 显示的控件
     * @param iWheelRegionChangeCallback 回调
     */
    public void handleRegionCheck(View parentView, TextView showView, boolean showDistricts,
                                  IWheelRegionChangeCallback iWheelRegionChangeCallback) {
        if (parentView == null) {
            return;
        }
        RegionWheel regionWheel;
        if (!TextUtils.isEmpty(showView.getText().toString())) {
            String[] regions = showView.getText().toString().split("\\s");
            if (regions.length > 2) { // 省市区全
                regionWheel = new RegionWheel(context, null, regions[INDEX_PROVINCE],
                        regions[INDEX_CITY], regions[INDEX_REGION], showDistricts);
            } else if (regions.length == 2) {// 特殊的，只有省份如澳门，香港
                regionWheel = new RegionWheel(context, null, regions[INDEX_PROVINCE],
                        regions[INDEX_CITY], null, showDistricts);
            } else {
                regionWheel = new RegionWheel(context, showDistricts);
            }
        } else {
            regionWheel = new RegionWheel(context, showDistricts);
        }
        SelectPopWindow pw = new SelectPopWindow(context, parentView,
                regionWheel.getAreaView());
        RegionClickListener regionClickListener = new RegionClickListener(pw, showView,
                iWheelRegionChangeCallback);
        regionWheel.setWheelFinishListener(regionClickListener);
        pw.setConfirmListener(regionClickListener);
    }


    /**
     * 处理选择省市
     *
     * @param parentView 父控件
     * @param showView 显示的控件
     */
    public void handleRegionCheck(View parentView, TextView showView) {
        handleRegionCheck(parentView, showView, null);
    }

    /**
     * 处理选择省市
     */
    class RegionClickListener implements RegionWheel.OnWheelFinishListener, View.OnClickListener {

        private final TextView showView;

        private String currentValue;

        private String currentCode;

        private String currentGjCode;

        private final SelectPopWindow pw;

        private final IWheelRegionChangeCallback iWheelRegionChangeCallback;

        public RegionClickListener(SelectPopWindow pw, TextView showView,
                                   IWheelRegionChangeCallback iWheelRegionChangeCallback) {
            this.showView = showView;
            this.pw = pw;
            this.iWheelRegionChangeCallback = iWheelRegionChangeCallback;
        }

        @Override
        public void onClick(View v) {
            if (showView != null) {
                showView.setText(currentValue);
            }
            if (iWheelRegionChangeCallback != null) {
                iWheelRegionChangeCallback.wheelRegionChangeCallback(currentValue, currentCode, currentGjCode);
            }
            pw.dismiss();
        }

        @Override
        public void OnChange(String currentValue, String currentCode, String currentGjCode) {
            this.currentValue = currentValue;
            this.currentCode = currentCode;
            this.currentGjCode = currentGjCode;
        }
    }
}
